<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .box {
            width: 800px;
            height: 500px;
            position: relative;
            margin: 50px auto;
            cursor: pointer;
            overflow: hidden;

        }

        ul {
            display: flex;
            position: relative;
            left: 0;
            transition: left .2s linear;

        }

        ol {
            display: flex;
            position: absolute;
            bottom: 10px;
            width: 100%;
            left: 0;
            justify-content: center;
        }

        ol>li {
            width: 10px;
            height: 10px;
            background-color: #fff;
            margin: 0 8px;
        }

        ol>li.action {
            background-color: black;
        }

        img {
            width: 800px;
            height: 500px;
            border-radius: 5px;
            display: block;
            object-fit: cover;
        }

        .box span {
            border: 1px solid #000;
            padding: 0 10px;
            position: absolute;
            line-height: 40px;
            top: calc(50% - 20px);
            background-color: rgba(255, 255, 255, 0.595);
        }

        .prev {
            left: 0;
        }

        .next {
            right: 0;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <!-- <li>
                <img src="./小姐 姐8.jpeg" alt="">
            </li> -->
        </ul>
        <ol>
            <!-- <li></li> -->
        </ol>
        <span class="prev">上一张</span>
        <span class="next">下一张</span>
    </div>
    <script>
        var image = ['./小姐姐8.jpeg', './小姐姐9.jpeg', './小姐姐11.jpeg', './小姐姐8.jpeg',];

        var ul = document.querySelector(".box > ul");
        var ol = document.querySelector(".box > ol");
        var box = document.querySelector(".box");

        var str = "", str1 = "";
        for (var k = 0; k < image.length; k++) {
            // console.log(image[k]);
            str += `<li><img class="boximg" src="${image[k]}" alt=""></li>`;
            if (k == 0) {
                str1 += `<li class="action"></li>`
            } else if (k < image.length - 1) {
                str1 += `<li></li>`
            }
        }
        ul.innerHTML = str;
        ol.innerHTML = str1;
        ul.style.width = image.length * box.offsetWidth + "px";
        // 点击上一张下一张
        // 自动轮播
        // 取消自动轮播
        // 点击小圆点
        var prev = document.querySelector(".prev");
        var next = document.querySelector(".next");
        var ollis = document.querySelectorAll(".box > ol > li");
        // 当前显示图片的索引值
        var i = 0;
        next.onclick = nextFun;
        // 封装函数
        function nextFun() {
            i++;
            // 设置ul的移动
            // 设置ul的移动
            ul.style.transitionDuration = "0.2s";
            // ul移动的距离
            var l = i * -box.offsetWidth;
            ul.style.left = l + "px";
            // 在点击下一张时,判断如果是最后一张了(复制的第一张)
            // 需要在动画执行完成后瞬间跳到第一张
            if (i == image.length - 1) { //i == 3
                i = 0;
                setTimeout(function () {
                    ul.style.transitionDuration = "0s";
                    ul.style.left = "0px";
                }, 200)
            }
            yuan()
        }
        prev.onclick = function () {
            i--;
            if (i == -1) {
                i = 2;
                ul.style.transitionDuration = "0.s";
                var l = (i + 1) * -box.offsetWidth;
                ul.style.left = l + "px";
            }
            yuan()
            setTimeout(function () {
                ul.style.transitionDuration = "0.2s";
                var l = i * -box.offsetWidth;
                ul.style.left = l + "px";
            }, 10)
        }
        function yuan() {
            for (let n = 0; n < ollis.length; n++) {
                ollis[n].classList.remove("action");
            }
            ollis[i].classList.add("action");
        }
        for (let m = 0; m < ollis.length; m++) {
            ollis[m].onclick = function () {
                i = m;
                ul.style.transitionDuration = "0.2s";
                var l = i * -box.offsetWidth;
                ul.style.left = l + "px";
                yuan()
            }
        }
        var time = setInterval(nextFun, 2000);
        box.onmouseenter = function () {
            clearInterval(time);
        }
        box.onmouseleave = function () {
            time = setInterval(nextFun, 2000);
        }
    </script>
</body>

</html>